<?php
/**
 * 开奖类型处理基类
 *
 * 
 *
 *
 * @copyright  Copyright (c) 2014-2015 TuntronKJ Inc. (http://www.tuntron.com)
 * @license    http://www.tuntron.com
 * @link       http://www.tuntron.com
 * @since      File available since Release v1.1
 */
defined('InTuntronKJ') or exit('Access Invalid!');

class kjtypeModel extends Model{
	public function __construct(){
		parent::__construct('type');
	}

	/**
	 * 读取一条类型信息
	 *
	 * @param string $type 类型ID
	 * @return array 数组格式的返回结果
	 */
	public function getRowType($type){
		$param	= array();
		$param['table']	= 'type';
		$param['where']	= "type='".$type."'";
		$result	= Db::select($param);
		if(is_array($result) and is_array($result[0])){
			return $result[0];
		}
		return false;
	}

	/**
	 * 获取一条类型信息
	 * @param unknown $condition
	 * @param string $field
	 */
	public function getTypeInfo($condition, $field = '*'){
		return $this->field($field)->where($condition)->find();
	}

	public function getGameLastNo($typeid, $time=null){

		if($time===null) $time=time();
		$atime=date('H:i:s', $time);
		$param['table']	= 'data_time';
		$param['where']	= "type='".$typeid."' and actionTime <= '".$atime."'";
		$param['order']	= 'actionTime desc';
		$param['limit']	= 1;
		$result	= Db::select($param);

		if(!is_array($result) and !is_array($result[0])){
			$param['where']	= "type='".$typeid."'";
			$result	= Db::select($param);
			$time=$time+24*3600;
		}

		/*函数处理
		$types=$this->getTypes();
		if(($fun=$types[$type]['onGetNoed']) && method_exists($this, $fun)){
			$this->$fun($return['actionNo'], $return['actionTime'], $time);
		}
		*/
		return $result[0];
	}
	
	/**
	 * 读取系统设置列表
	 *
	 * @param 
	 * @return array 数组格式的返回结果
	 */
	public function getListSetting(){
		$param = array();
		$param['table'] = 'setting';
		$result = Db::select($param);
		/**
		 * 整理
		 */
		if (is_array($result)){
			$list_setting = array();
			foreach ($result as $k => $v){
				$list_setting[$v['name']] = $v['value'];
			}
		}
		return $list_setting;
	}
	
	/**
	 * 更新信息
	 *
	 * @param array $param 更新数据
	 * @return bool 布尔类型的返回结果
	 */
	public function updateSetting($param){
		if (empty($param)){
			return false;
		}

		if (is_array($param)){
			foreach ($param as $k => $v){
				$tmp = array();
				$specialkeys_arr = array('statistics_code');
				$tmp['value'] = (in_array($k,$specialkeys_arr) ? htmlentities($v,ENT_QUOTES) : $v);
				$where = " name = '". $k ."'";
				$result = Db::update('setting',$tmp,$where);
				if ($result !== true){
					return $result;
				}
			}
			H('setting',true);
			@unlink(BASE_DATA_PATH.DS.'cache'.DS.'setting.php');
			return true;
		}else {
			return false;
		}
	}


	/**
	 * 构造查询条件
	 *
	 * @param array $condition
	 * @return string
	 */
	private function getCondition($condition = array()){
		$return	= '';
		$time   = time();
		if($condition['enable'] != ''){
			$return	.= " and enable='".$condition['enable']."'";
		}
		if($condition['isDelete'] != ''){
			$return	.= " and isDelete='".$condition['isDelete']."'";
		}
		if($condition['sort'] != ''){
			$return	.= " and sort='".$condition['sort']."'";
		}
		if($condition['name'] != ''){
			$return	.= " and name='".$condition['name']."'";
		}
		if($condition['codeList'] != ''){
			$return	.= " and codeList='".$condition['codeList']."'";
		}
		if($condition['actionNo'] != ''){
			$return	.= " and actionNo='".$condition['actionNo']."'";
		}
		if($condition['startTime'] != ''){
			$return	.= " and startTime='".$condition['startTime']."'";
		}
		if($condition['stopTime'] != ''){
			$return	.= " and stopTime='".$condition['stopTime']."'";
		}

		if($condition['actionTime'] != ''){
			$return	.= " and actionTime='".$condition['actionTime']."'";
		}

		if($condition['type'] != ''){
			$return	.= " and type='".$condition['type']."'";
		}
		if($condition['time'] != ''){
			$return	.= " and time='".$condition['time']."'";
		}
		if($condition['number'] != ''){
			$return	.= " and number!='".$condition['number']."'";
		}
		if ($condition['data'] != '') {
			$return .= " and data='".$condition['data']."'";
		}

		return $return;
	}

	
}
